{% macro render_nav(user_info, logout_url) %}
<div id="navcontainer">
<ul id="navlist">
<!--	<li id="active"><a id="current" href="/">Add transaction</a></li>  -->
	<li><a href="/">Add transaction</a></li>
	<li><a href="/accounts">Accounts</a></li>
	<li><a href="/all">All transactions</a></li>
	<li><a href="#">{{ user_info.user.nickname() }}</a></li>
	<li><a href="{{ logout_url }}">Logout</a></li>
</ul>
</div>
{% endmacro %}

{% macro account_link(account) %}
<a href="/account?id={{ account.key().id() }}">{{ account.name }}</a>
{% endmacro %}


{% macro render_field(field, tabindex=0) %}
  {{ field.label }}
  {{ field(tabindex=tabindex) }}
  {% if field.errors %}
    <ul class="error">
    {% for error in field.errors %}
      <li class="error">{{ error }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}

{% macro render_transaction_form(form, edit_mode) %}
	<form name="transaction" action="/add" method="post">
		{{ render_field(form.date, 1) }}
		{{ render_field(form.income, 5) }}
		<br/>
		{{ render_field(form.amount, 2) }}
		{{ render_field(form.to_amount, 6) }}
		<br/>
		{{ render_field(form.account, 3) }}
		{{ render_field(form.to_account, 7) }}
		<br/>
		{{ render_field(form.tags, 4) }}
		<br/> <br/>
		{% if not edit_mode %}
		<input type="submit" value="Add" tabindex="8" />
		{% else %}
		{{ form.id|safe }}
		<input type="submit" value="Change" tabindex="9" />
		<input type="button" value="Cancel" tabindex="9" onClick="parent.location='/'" />
		{% endif %}
	</form>
	<br/>
	<script type="text/javascript">
		document.transaction.date.focus();
	</script>
{% endmacro %}

{% macro render_account_form(form, edit_mode) %}
	<form method=post action="/accounts">
		{{ render_field(form.name) }}
		<br/>
		{{ render_field(form.currency) }}
		<br/>
		{{ render_field(form.opened) }}
		<br/>
		{{ render_field(form.initial) }}
		<br/>
		{% if not edit_mode %}
		<input type="submit" value="Add"/>
		{% else %}
		{{ form.id|safe }}
		<input type="submit" value="Change" />
		<input type="button" value="Cancel" onClick="parent.location='/accounts'" />
		{% endif %}
	</form>
{% endmacro %}

{% macro render_transactions(transactions) %}
<table class="transactions" cellspacing="0">
	<tr>
		<th>Date</th>
		<th>Amount</th>
		<th colspan="2">Account</th>
		<th>To amount</th>
		<th colspan="2">To account</th>
		<th>Tags</th>
		<th></th>
	</tr>
	{% for t in transactions %}
	<tr>
		<td class="right">{{ t.date }}</td>
		<td class="right">{{ t.amount|cents }}</td>
		<td class="tight">{{ t.account.currency }}</td>
		<td>{{ account_link(t.account) }}</td>
		<td class="right">{{ t.to_amount|cents }}</td>
		<td class="tight">{{ t.to_account.currency }}</td>
		<td>{{ t.to_account.name|default('-') }}</td>
		<td>{{ t.tags|join(',') }}</td>
		<td><a href="/?id={{ t.key().id() }}">edit</a></td>
	</tr>
	{% endfor %}
</table>
{% endmacro %}

{% macro render_accounts(accounts) %}
<table class="accounts" cellspacing="0">
	<tr>
		<th rowspan="2">Name</th>
		<th rowspan="2">Currency</th>
		<th colspan="2">Daily spending</th>
		<th colspan="2">Balance</th>
	</tr>
    <tr>
        <th>last month</th>
        <th>all time</th>
        <th>last month</th>
        <th>all time</th>
        <th></th>
    </tr>
	{% for a in accounts %}
	<tr>
		<td class="center">{{ a.name }}</td>
		<td class="center">{{ a.currency }}</td>
		<td class="right">{{ a.daily_spending(last_month=True)|cents }}</td>
		<td class="right">{{ a.daily_spending()|cents }}</td>
		<td class="right">{{ a.balance(last_month=True)|integer }}</td>
		<td class="right">{{ a.balance()|integer }}</td>
		<td><a href="/accounts?id={{ a.key().id() }}">edit</a></td>
	</tr>
	{% endfor %}
</table>
{% endmacro %}

{% macro render_total_balance(total_balance) %}
<table cellspacing="0">
	{% for currency,amount in total_balance.items() %}
	<tr>
		<td class="right">{{ amount }}</td>
		<td>{{ currency }}</td>
	</tr>
	{% endfor %}
</table>
{% endmacro %}

